<?php
/* ******************************
 *	Client Object List aJax actions
 * ******************************
 */

include('../../../includes/classes/core.php');

$action		= $_REQUEST['act'];
$error		= '';
$data		= '';
$user_id	= $_COOKIE['USERID'];

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	    $list_id	= $_REQUEST['id'];
		$page		= GetPage(GetObjectList($list_id));
		
		$data		= array('page'	=> $page);

        break;
    case 'get_list':
	    $count		= $_REQUEST['count'];
	    $hidden		= $_REQUEST['hidden'];
	    $local_id	= $_REQUEST['local_id'];
	    
	    $rResult = mysql_query("SELECT		`client_objects`.`id`,
	    									`client_objects`.`name`,
											`client_objects`.`address`,
											`district_type`.`name`,
											`client_objects`.`contact_person`,
											`client_objects`.`phone_number`
								FROM		`client_objects` 
								LEFT JOIN 	`client` ON	`client_objects`.`client_id` = `client`.`id`
								LEFT JOIN	`district_type` ON `district_type`.`id`  = `client_objects`.`district_type`
								WHERE		`client`.`id` = '$local_id' && `client_objects`.`actived` = 1");
		
		$data = array(
			"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'save_object':
		$object_id		= $_REQUEST['id'];
		$local_id		= $_REQUEST['lci'];
		
		$arr = array(
				"name"				=> htmlspecialchars($_REQUEST['on'], ENT_QUOTES),
				"address"			=> htmlspecialchars($_REQUEST['oa'], ENT_QUOTES),
				"district"			=> htmlspecialchars($_REQUEST['od'], ENT_QUOTES),
				"contact_person"	=> htmlspecialchars($_REQUEST['ocp'], ENT_QUOTES),
				"phone_number"		=> htmlspecialchars($_REQUEST['opn'], ENT_QUOTES),
				"mail"				=> htmlspecialchars($_REQUEST['om'], ENT_QUOTES),
				"comment"			=> htmlspecialchars($_REQUEST['oc'], ENT_QUOTES)
		);
		
		if($object_id == ''){
			AddObjectList($user_id, $local_id, $arr);
		}else{
			SaveObjectList($object_id, $user_id, $arr);
		}

        break;
    case 'disable':
		$list_id = $_REQUEST['id'];
		DisableObjectList($list_id);
		
        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Client Object List Functions
 * ******************************
 */

function AddObjectList($user_id, $local_id, $arr)
{
	mysql_query("INSERT INTO `client_objects`
					(`user_id`, `client_id`, `name`, `address`, `district_type`, `contact_person`, `phone_number`, `mail`, `comment`) 
				 VALUES
					($user_id, $local_id, '$arr[name]', '$arr[address]', '$arr[district]', '$arr[contact_person]', '$arr[phone_number]', '$arr[mail]', '$arr[comment]')");
}

function SaveObjectList($object_id, $user_id, $arr) 
{
	mysql_query("UPDATE
	    			`client_objects`
				 SET
					`user_id`			= $user_id,
					`name`				= '$arr[name]',
					`address`			= '$arr[address]',	
					`district_type`     = '$arr[district]',			
					`contact_person`	= '$arr[contact_person]',
					`phone_number`		= '$arr[phone_number]',
					`mail`				= '$arr[mail]',
					`comment`			= '$arr[comment]'
				 WHERE
					`id` = $object_id");
}

function DisableObjectList($list_id)
{
    mysql_query("	UPDATE
				    	`client_objects`
				    SET
					    `actived`	= 0
				    WHERE
				    	`id` = $list_id");
}

function GetObjectList($list_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT	`id`,
    												`name`,													
													`address`,
    												`district_type`,
													`contact_person`,
													`phone_number`,
    												`mail`,
    												`comment`
										FROM		`client_objects`
										WHERE		`id` = $list_id"));
	return $res;
}

function GetDistrict($object_district){
	$data = '';
	$req = mysql_query("SELECT	`district_type`.`id` as `id`,
								`district_type`.`name` as `name`
						FROM	`district_type`");
								
	if($object_district == ''){
		$data = '<option value="0" selected="selected"></option>';
	}
	
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_district){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}
	
	return $data;	
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
    		<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="obj_name">დასახელება</label></td>
					<td>
						<input type="text" id="obj_name" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['name'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="obj_address">მისამართი</label></td>
					<td>
						<input type="text" id="obj_address" class="idle address" onblur="this.className=\'idle address\'" onfocus="this.className=\'activeField address\'" value="' . $res['address'] . '" />
					</td>
				</tr>
				<tr>
					<th><label for="obj_district">უბანი</label></th>
					<td><select id="obj_district" class="idls">' . GetDistrict($res['district_type']) . '</select></td>
				</tr>								
				<tr class="comment">
					<td style="width: 170px;" valign="top"><label for="obj_comment">შენიშვნა</label></td>
					<td>
						<textarea id="obj_comment" class="idle large" cols="40" rows="3">' . $res['comment'] . '</textarea>
					</td>
				</tr>
			</table>
        </fieldset>
		<fieldset>
	    	<legend>თანამშრომლები</legend>
		    <div class="inner-table">
			    <div id="dt_example" class="ex_highlight_row">
			        <div id="container" class="overhead_container">
			        	<div id="button_area">
			        		<button id="add_button_person">დამატება</button><button id="delete_button_person">წაშლა</button>
			        	</div>
			            <div id="dynamic">
			                <table class="display" id="person_list">
			                    <thead>
			                        <tr id="datatable_header">				                        
			                            <th>ID</th>
			                            <th style="width: 40%">სახელი</th>
			                            <th style="width: 60%">ტელ. ნომერი</th>
			                            <th class="min">ელ-ფოსტა</th>
			                            <th class="check">#</th>
			                        </tr>
			                    </thead>
			                    <thead>
			                        <tr class="search_header">
			                            <th class="colum_hidden">
			                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
			                            </th>
			                            <th>
			                            	<input type="text" name="search_name" value="ფილტრი" class="search_init" />
			                            </th>
			                            <th>
			                            	<input type="text" name="search_type" value="ფილტრი" class="search_init" />
			                            </th>
			                            <th>
			                            	<input type="text" name="search_price" value="ფილტრი" class="search_init" />
			                            </th>
										<th>
											<input type="checkbox" name="check-all" id="check-all-printer">
										</th>
			                        </tr>
			                    </thead>
			                </table>
			            </div>
			        </div>
			    </div>
			</div>
		</fieldset>
		<!-- ID -->
		<input type="hidden" id="object_list_id" value="' . $res['id'] . '" />
				
		<!-- jQuery Dialog -->
	    <div id="add-edit-person-form" class="form-dialog" title="თანამშრომელი">
	    	<!-- aJax -->
		</div>
    </div>
    ';
	return $data;
}

?>